package com.lanlian.cayman.activity;

import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnDrawListener;
import com.joanzapata.pdfview.listener.OnLoadCompleteListener;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import com.lanlian.cayman.R;
import com.lanlian.commonlib.base.BaseActivity;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/9/8 16:43
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
public class AgreementActivity extends BaseActivity implements OnPageChangeListener, OnLoadCompleteListener, OnDrawListener {

    private int type;
    private int tag;
    private long time;

    private PDFView pdfView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        switch (type) {
            case 1:
                setContentView(R.layout.activity_agreement_risk, true);
                pdfView = findViewById(R.id.pdfView);
                displayFromAssets("风险揭示书.pdf");
                break;
            case 2:
                setContentView(R.layout.activity_agreement_trade, true);
                break;
            case 3:
                setContentView(R.layout.activity_agreement_risk, true);
                pdfView = findViewById(R.id.pdfView);
                displayFromAssets("合作协议书.pdf");
                break;
            default:
                break;
        }
        initView();
        time = System.currentTimeMillis();
        mImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tag == 1) {
                    finish();
                } else {
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - time > 3000) {
                        finish();
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        type = getIntent().getIntExtra("type", 1);
        tag = getIntent().getIntExtra("tag", 0);
    }

    private void displayFromAssets(String assetFileName ) {
        pdfView.fromAsset(assetFileName)   //设置pdf文件地址
                .defaultPage(1)         //设置默认显示第1页
                .onPageChange(this)     //设置翻页监听
                .onLoad(this)           //设置加载监听
                .onDraw(this)            //绘图监听
                .showMinimap(false)     //pdf放大的时候，是否在屏幕的右上角生成小地图
                .swipeVertical(true)  //pdf文档翻页是否是垂直翻页，默认是左右滑动翻页
                .enableSwipe(true)   //是否允许翻页，默认是允许翻页
                .load();
    }

    @Override
    protected void initView() {
        super.initView();
        switch (type) {
            case 1:
                setTitle("交易风险揭示书");
                break;
            case 2:
                setTitle("交易须知");
                break;
            case 3:
                setTitle("投资合作协议书");
                break;
            default:
                break;
        }
    }


    @Override
    public void onBackPressed() {
        if (tag == 1) {
            super.onBackPressed();
        } else {
            long currentTime = System.currentTimeMillis();
            if (currentTime - time > 3000) {
                super.onBackPressed();
            }
        }
    }

    @Override
    public void onPageChanged(int page, int pageCount) {

    }

    @Override
    public void loadComplete(int nbPages) {

    }

    @Override
    public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {

    }
}
